﻿using Client.ICommandNotify;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.ViewModel.Tender
{
    public class TenderViewModel : NotifyBase
    {
        public TenderViewModel()
        {
            Selectcommand = new Command(ReverseDoExecute, ReverseCanExecute);
            Companycommand = new Command(CompanyDoExecute, CompanyCanExecute);
        }
        #region 命令
        public Command Selectcommand { get; set; }

        public Command Companycommand { get; set; }
        #endregion
        #region 方法
        private bool ReverseCanExecute()
        {
            return string.IsNullOrWhiteSpace(DocumentNumber) ? false : true;
        }
        private bool CompanyCanExecute()
        {
            return string.IsNullOrWhiteSpace(DocumentNumber) ? false : true;
        }
        private async void ReverseDoExecute(object obj)
        {
            var DbContext_ = HelpTools.IOC.Getobject<SqlServer.DbContext>();
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            string strconfig = HelpTools.IOC.Getobject<IConfiguration.IConfigurationHelp>().GetSetUpModel().Where(o => o.DatabaseName == Title)?.FirstOrDefault()?.ConnectingString;
            if (string.IsNullOrWhiteSpace(strconfig))
            {
                Dialog_.OpenMessage("数据连接字符串为空,请检查数据库配置是否正确");
                return;
            }
            DbContext_.SqlConfigstr = strconfig;
            Dialog_.OpenDialog = true;
            ResponseData = await Task.Run(async () =>
            {
                string str = "";
                if ((obj as string) == "1")
                {
                    str = "ReleaseStatus = 0";
                }
                else
                {
                    str = "IsTenderPlat = 1";
                }
                int index = DbContext_.Update($"UPDATE Ted_Tender set {str} WHERE BillNo='{DocumentNumber}'");
                await Task.Delay(1000);
                if (index > 0)
                {
                    return "操作成功";
                }

                return "操作失败";
            });
            Dialog_.OpenDialog = false;

        }
        //修改单位id
        private void CompanyDoExecute(object obj)
        {
            var DbContext_ = HelpTools.IOC.Getobject<SqlServer.DbContext>();
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            string strconfig = HelpTools.IOC.Getobject<IConfiguration.IConfigurationHelp>().GetSetUpModel().Where(o => o.DatabaseName == Title)?.FirstOrDefault()?.ConnectingString;
            DbContext_.SqlConfigstr = strconfig;
            if (string.IsNullOrWhiteSpace(CompanyId) == false)
            {
                var data = DbContext_.Where($"SELECT  a.ContractSectionId, a.CompanyId,a.CompanyName FROM [dbo].[Ted_ZhaoBiaoBaoMinAudit] a WHERE a.BillId='{DocumentNumber}'").Select().Select(o =>
                 {
                     return new
                     {
                         ContractSectionId = o["ContractSectionId"].ToString(),
                         CompanyId = o["CompanyId"].ToString(),
                         CompanyName = o["CompanyName"].ToString()
                     };
                 });
                if (data != null && data.Any() && string.IsNullOrWhiteSpace(data.FirstOrDefault()?.ContractSectionId) == false && string.IsNullOrWhiteSpace(data.FirstOrDefault()?.CompanyName) == false)
                {
                    DbContext_.Update($"UPDATE Ted_ZhaoBiaoBaoMinAudit SET CompanyId= '{CompanyId}' WHERE BillId = '{DocumentNumber}'");
                    DbContext_.Update($"UPDATE Ted_Zhongbiaohouxuan SET CandidateId = '{CompanyId}' WHERE ContractSectionId = '{data.FirstOrDefault().ContractSectionId}' AND Candidate='{data.FirstOrDefault().CompanyName}'");
                    DbContext_.Update($"UPDATE Ted_ZhongBiaoBidder SET BidderId = '{CompanyId}' WHERE ContractSectionId = '{data.FirstOrDefault().ContractSectionId}' AND Bidder='{data.FirstOrDefault().CompanyName}'");
                }
                else
                {
                    WPFDevelopers.Minimal.Controls.MessageBox.Show("处理失败", "消息", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                WPFDevelopers.Minimal.Controls.MessageBox.Show("处理成功", "消息", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            var data1 = DbContext_.Where($"SELECT  a.ContractSectionId, a.CompanyId,a.CompanyName FROM [dbo].[Ted_ZhaoBiaoBaoMinAudit] a WHERE a.BillId='{DocumentNumber}'").Select().Select(o =>
            {
                return new
                {
                    ContractSectionId = o["ContractSectionId"].ToString(),
                    CompanyId = o["CompanyId"].ToString(),
                    CompanyName = o["CompanyName"].ToString()
                };
            });
            if (data1 != null && data1.Any() && string.IsNullOrWhiteSpace(data1.FirstOrDefault()?.ContractSectionId) == false && string.IsNullOrWhiteSpace(data1.FirstOrDefault()?.CompanyName) == false && string.IsNullOrWhiteSpace(data1.FirstOrDefault()?.CompanyId) == false)
            {
                DbContext_.Update($"UPDATE Ted_Zhongbiaohouxuan SET CandidateId = '{data1.FirstOrDefault()?.CompanyId}' WHERE ContractSectionId = '{data1.FirstOrDefault().ContractSectionId}' AND Candidate='{data1.FirstOrDefault().CompanyName}'");
                DbContext_.Update($"UPDATE Ted_ZhongBiaoBidder SET BidderId = '{data1.FirstOrDefault()?.CompanyId}' WHERE ContractSectionId = '{data1.FirstOrDefault().ContractSectionId}' AND Bidder='{data1.FirstOrDefault().CompanyName}'");
            }
            else
            {
                WPFDevelopers.Minimal.Controls.MessageBox.Show("处理失败", "消息", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            WPFDevelopers.Minimal.Controls.MessageBox.Show("处理成功", "消息", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        #endregion
        #region 属性
        private string Title_ = "招标";
        //标题
        public string Title
        {
            get { return Title_; }
            set { Title_ = value; }
        }

        private string DocumentNumber_;
        //单据编号
        public string DocumentNumber
        {
            get { return DocumentNumber_; }
            set { DocumentNumber_ = value; }
        }
        private string CompanyId_;

        public string CompanyId
        {
            get { return CompanyId_; }
            set { CompanyId_ = value; }
        }

        private string ResponseData_;
        //HTTP请求响应数据
        public string ResponseData
        {
            get { return ResponseData_; }
            set
            {
                ResponseData_ = value;
                SetProperty(ref DocumentNumber_, DocumentNumber);
            }
        }

        #endregion
    }
}
